<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of news
 *
 * @author HungPV-PC
 */
class news_model extends CI_Model{
    function __construct() {
        parent::__construct();
    }
    
    function get_count() {
        return $this->db->count_all('news');
    }
    
    function get($id) {
        $query = $this->db->get_where('news', array('id' => $id));
        $result = $query->result();

        if (count($result) > 0) {
            return $result[0];
        } else {
            return NULL;
        }
    }
    
    function get_news($ofset = -1, $limit = 0) {
        if ($ofset == -1) {
            $query = $this->db->get_where('news');
        } else {
            $query = $this->db->get('news', $limit,$ofset);
        }
        return $query->result();
    }
    
    

    function add($name, $description, $content,$image) {
        $data = array(
            'name' => $name,
            'description' => $description,
            'content' => $content,
            'date' => date('Y-m-d H:i:s'),
            'image' => $image
        );

        $this->db->insert('news', $data);
        return $this->db->insert_id();
    }
    
    function edit($id, $name, $description,$content,$image) {
        $data = array(
            'name' => $name,
            'description' => $description,
            'content' => $content,
            'image' => $image
        );
        $this->db->where('id', $id);
        $this->db->update('news', $data);
    }
    
    function delete($id) {
        $this->db->delete('news', array('id' => $id));
    }
}

?>
